using HappyPuffy.Structs;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.Collections.Generic;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch; 

namespace HappyPuffy
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public class Game1 : Game
    {
        GraphicsDeviceManager _graphics;
        SpriteBatch _spriteBatch;
        SpriteBase _gameSprite;

        Texture2D _test = null;

        float _speedRatio = 1; 

        KeyboardState currentKeyboardState  ; 

        KeyboardState previousKeyboardState; 

        Dictionary<GameEnum, SpriteBase> _dictSprite = new Dictionary<GameEnum, SpriteBase>(4);


        public Game1()
        {
            _graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            // TODO: Add your initialization logic here

            TouchPanel.EnabledGestures = GestureType.FreeDrag; 
            
            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            // Create a new SpriteBatch, which can be used to draw textures.
            _spriteBatch = new SpriteBatch(GraphicsDevice);


            _test = this.Content.Load<Texture2D>("PuffoSalta");

            SpriteBase sprite = null;

            sprite = new SpriteStart(this.Content.Load<Texture2D>("PuffoSalta"), Vector2.Zero, Color.White );
            _dictSprite.Add(GameEnum.Start, sprite );

            sprite = new SpriteGame(this.Content.Load<Texture2D>("PufPausa"), Vector2.Zero, Color.White);
            _dictSprite.Add(GameEnum.Pause, sprite);

            sprite = new SpriteGame(this.Content.Load<Texture2D>("PuffoSaluta"), Vector2.Zero, Color.White);
            _dictSprite.Add(GameEnum.Game, sprite);

            sprite = new SpriteGame(this.Content.Load<Texture2D>("GrandePuffo"), Vector2.Zero, Color.White);
            _dictSprite.Add(GameEnum.End, sprite);

            _gameSprite =  _dictSprite[GameEnum.Start];

             //

        }

        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        /// <summary>
        /// Allows the game to run logic such as updating the world,
        /// checking for collisions, gathering input, and playing audio.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Update(GameTime gameTime)
        {
            // TODO: Add your update logic here
            currentKeyboardState = Keyboard.GetState();

            float x=0, y=0;

            if (currentKeyboardState.IsKeyDown(Keys.A))
                _speedRatio += 0.5F;
            else if (currentKeyboardState.IsKeyDown(Keys.D))
                _speedRatio -= 0.5F;

            _speedRatio = _speedRatio < 1 ? 1 :  (_speedRatio > 5 ? 5: _speedRatio ) ;


            if (currentKeyboardState.IsKeyDown(Keys.Left))
                x = -2 * _speedRatio ;
            else if (currentKeyboardState.IsKeyDown(Keys.Right))
                x = 2 * _speedRatio;

            if (currentKeyboardState.IsKeyDown(Keys.Up))
                y = -2 * _speedRatio;
            else if (currentKeyboardState.IsKeyDown(Keys.Down))
                y = 2 * _speedRatio; 

            _gameSprite.AddPosition(x,y);

            base.Update(gameTime);
        }

        /// <summary>
        /// This is called when the game should draw itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        protected override void Draw(GameTime gameTime)
        {
            
            GraphicsDevice.Clear(Color.DarkCyan);

            _spriteBatch.Begin();
            // TODO: Add your drawing code here

            _gameSprite.Draw(_spriteBatch);


            _spriteBatch.End();


            base.Draw(gameTime);
            

           

        }





        internal enum GameEnum
	    {
	         Start, Game,Pause,End
	    }

    }
}
